<template>
	<view>
    <!-- 自定义搜索组件 -->
   <my-search @click='getSearch'></my-search>
    
    <!-- 滚动条区域 -->
    <view class="scroll-container">
      <scroll-view class="scroll-left" scroll-y="true" :style="{height: wh + 'px'}" >
        <block v-for="(item,i) in cateList" :key='i'>
          <view 
            :class="['left-scroll-item', i === active ? 'active' : '' ]" 
            @click="activeChanged(i)"
            >{{ item.cat_name }}
          </view>
        </block>
      </scroll-view>
     <scroll-view class="scroll-right" scroll-y="true" :style="{height: wh + 'px'}" :scroll-top='scrollTop'>
       <block v-for="(item2, i2) in cateLivl2" :key='i2'>
          <view class="right-scroll-item2">/{{ item2.cat_name }}/</view>
          <!-- 动态渲染三级分类 -->
          <view class="right-scroll-list3">
            <view class="right-scroll-item3" v-for="(item3, i3) in item2.children" :key='i3' @click="gotoGoodsList(item3)">
              <image :src="item3.cat_icon" ></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
       </block>

     </scroll-view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        // 当前设备可用高度
				wh:0,
        // 数据列表
        cateList: [],
        // 样式激活项的索引
        active: 0,
        // 二级分类的数据
        cateLivl2: [],
        // 滚动条位置
        scrollTop: 0
			};
		},
    onLoad(){
      const sysInfo = uni.getSystemInfoSync()
      console.log(sysInfo)
      this.wh = sysInfo.windowHeight - 50
      this.getCateList()
    },
    methods:{
      // 获取购物列表的函数方法
     async getCateList(){
       // const { data: res } = await uni.$http.get('/api/public/v1/categories')
       //  console.log(data)
       const { data: res } = await uni.$http.get('/api/public/v1/categories')
       // console.log(res)
       //请求失败
       if(res.meta.status !== 200) return uni.$showMsg()
       // 请求成功8
       this.cateList = res.message
       // 为二级数据分类
       // console.log(res.message[0].children)
       this.cateLivl2 = res.message[0].children
      },
      activeChanged(i){
        this.active = i
        this.cateLivl2 = this.cateList[i].children
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
      },
      // 点击三级函数页面跳转到商品列表的处理函数
      gotoGoodsList(item3){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid=' + item3.cat_id
        })
      },
      getSearch() {
      uni.navigateTo({
        url:'/subpkg/search/search'
      })
      }
    }
	}
</script>                          

<style lang="scss">
.scroll-container{
  display: flex;
  
  .scroll-left{
    width: 120px;
    
    .left-scroll-item{
      background-color: #F7F7F7;
      line-height: 60px;
      font-size: 16px;
      text-align: center;
      
      &.active{
        background-color: #FFFFFF;
        position: relative;
        
        &::before{
          content:'';
          display: block;
          width: 3px;
          height: 30px;
          background-color: #C00000;
          position:absolute;
          top: 50%;
          left: 0;
          transform: translateY(-50%);
        }
      }
    }
  }
}
.right-scroll-item{
  font-size: 12px;
  font-weight: bold;
  text-align: center;
  padding: 15px 0;
}
.right-scroll-item2{
  text-align: center;
}
.right-scroll-list3 {
  display: flex;
  flex-wrap: wrap;

  .right-scroll-item3 {
    width: 33.33%;
    margin-bottom: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;

    image {
      width: 60px;
      height: 60px;
    }

    text {
      font-size: 12px;
    }
  }
}
</style>
